﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;
using Shohdi.DataHandler;

public partial class OrganizationChartControl : System.Web.UI.UserControl
{
    public bool AutoPostBack
    {
        get
        {
            if (isParent)
            {

                return ddlDivision.AutoPostBack;
            }
            else
            {
                return ddlSection.AutoPostBack;
            }
        }

        set
        {
            if (isParent)
            {
                ddlDivision.AutoPostBack = value;
                ddlSection.AutoPostBack = value;
            }
            else
            {
                ddlDivision.AutoPostBack = true;
                ddlSection.AutoPostBack = value;
            }
        }
    }
    

    public bool isParent
    {
        get
        {
            if (ViewState["isParent"] == null)
            {
                ViewState["isParent"] = false;
            }

            return (bool)ViewState["isParent"];
        
        }
        set
        {
            ViewState["isParent"] = value;
            ddlSection.Visible = !isParent;
        }

    }


    public delegate void ItemChangedDel (object sender,EventArgs e) ;


    public event ItemChangedDel ItemChanged = null;

    protected virtual void OnItemChanged( EventArgs e)
    {
        if (ItemChanged != null)
            ItemChanged(this, e);

    }


    public DropDownList DDLCompany
    {
        get
        {
            return ddlCompany;
        }
    }


    public DropDownList DDLActivity
    {
        get
        {
            return ddlActivity;
        }
    }

    public DropDownList DDLSector
    {
        get
        {
            return ddlSector;
        }
    }


    public DropDownList DDLManagement
    {
        get
        {
            return ddlManagement;
        }
    }


    public DropDownList DDLDivision
    {
        get
        {
            return ddlDivision;
        }
    }


    public DropDownList DDLSection
    {
        get
        {
            return ddlSection;
        }
    }

    /// <summary>
    /// this function will return a value of organization chart control
    /// </summary>
    /// <returns>the id of the lowest degree selected depart</returns>
    public long get()
    {
        if (ddlActivity.SelectedValue == "")
        {
            bindBusiness();
            bindSector();
            bindManagements();
            bindDivisions();
            bindSections ();
           
        }
        
            if (!isParent)
            {
                if (ddlSection.SelectedValue != "-1")
                {
                    return long.Parse(ddlSection.SelectedValue);
                }

            }

            if (ddlDivision.SelectedValue != "-1")
            {
                return long.Parse(ddlDivision.SelectedValue);
            }

            if (ddlManagement.SelectedValue != "-1")
            {
                return long.Parse(ddlManagement.SelectedValue);
            }


            if (ddlSector.SelectedValue != "-1")
            {
                return long.Parse(ddlSector.SelectedValue);
            }

            if (ddlActivity.SelectedValue != "-1")
            {
                return long.Parse(ddlActivity.SelectedValue);
            }
        
        


        return getMainOrg();

        


    }


    public static long getMainOrg()
    {
        DatabaseComponent.OrgnizationChartDB objOrgnizationChartDB = new DatabaseComponent.OrgnizationChartDB(ConnectionStrings.LMS);

        List<OrgnizationChart> lstOrgMostParent = objOrgnizationChartDB.Select("", " where [ParentID] is NULL ");

        List<long> lstParentIDs = new List<long>(from objNewIn in lstOrgMostParent select objNewIn.ID);


        if (lstParentIDs.Count > 0)
            return lstParentIDs[0];
        else
            return -1;
    }




    /// <summary>
    /// this function will set the organization chart control by ID of lowest child
    /// </summary>
    /// <param name="OrganizationChartID"></param>
    public void set(long OrganizationChartID)
    {
        OrgnizationChartDB objOrgnizationChartDB = new OrgnizationChartDB(ConnectionStrings.LMS);
        List<OrgnizationChart> lstOrgnizationChart = objOrgnizationChartDB.getOrgnizationChartByID(OrganizationChartID);
        if (lstOrgnizationChart.Count == 0)
        {
            if (ddlActivity.Items.Count == 0)
                bindBusiness();
            ddlActivity.SelectedValue = "-1";
            ddlActivity_SelectedIndexChanged(this, new EventArgs());

            return;


          
        }

        if (lstOrgnizationChart[0].ParentID == null || lstOrgnizationChart[0].isDeleted)
        {
            if (ddlActivity.Items.Count == 0)
                bindBusiness();
            ddlActivity.SelectedValue = "-1";
            ddlActivity_SelectedIndexChanged(this, new EventArgs());

            return;
        }






        List<OrgnizationChart> lstOrgMostParent = objOrgnizationChartDB.Select("", " where [ParentID] is NULL ");

        List<long> lstParentIDs = new List<long>(from objNewIn in lstOrgMostParent select objNewIn.ID);
        
       

        while (!lstParentIDs.Contains(lstOrgnizationChart[0].ParentID.Value))
        {
            List<OrgnizationChart> lstParent = objOrgnizationChartDB.getOrgnizationChartByID(lstOrgnizationChart[0].ParentID.Value);
            if (lstParent.Count > 0)
            {
                lstOrgnizationChart.Insert(0, lstParent[0]);
            }

        }

        try
        {
            if (ddlActivity.Items.Count == 0)
                bindBusiness();
            ddlActivity.SelectedValue = lstOrgnizationChart[0].ID.ToString();
            bindSector();
            ddlSector.SelectedValue = lstOrgnizationChart[1].ID.ToString();
            bindManagements();
            ddlManagement.SelectedValue = lstOrgnizationChart[2].ID.ToString();
            bindDivisions();
            ddlDivision.SelectedValue = lstOrgnizationChart[3].ID.ToString();
            bindSections();
            ddlSection.SelectedValue = lstOrgnizationChart[4].ID.ToString();

        }
        catch
        {
        }



    }




   





    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (isParent)
            //{
            //    ddlDivision.AutoPostBack = false;
            //    ddlSection.Visible = false;
                
            //}
            bindCompany();
            bindBusiness();
            bindSector();
            bindManagements();
            bindDivisions();
            bindSections();

            OnItemChanged(new EventArgs());
        }
    }

    /// <summary>
    /// this function will concatenate name with type
    /// </summary>
    /// <param name="lstOrgnizationChart">the list before concatination</param>
    /// <returns>the list after concatination</returns>
    protected List<OrgnizationChart> setNames(List<OrgnizationChart> lstOrgnizationChart)
    {
        for (int i = 0; i < lstOrgnizationChart.Count; i++)
        {
            if (lstOrgnizationChart[i].ID != -1)
            {
                lstOrgnizationChart[i].NameAR = lstOrgnizationChart[i].objChartType.TypeNameAR + " : " + lstOrgnizationChart[i].NameAR;
                lstOrgnizationChart[i].NameEN = lstOrgnizationChart[i].objChartType.TypeNameEN + " : " + lstOrgnizationChart[i].NameEN;
            }
        }


        return lstOrgnizationChart;
    }


    /// <summary>
    /// this function will fill the Company from database
    /// </summary>
    protected void bindCompany()
    {
        OrgnizationChart objOrganizationChart = new OrgnizationChart();
        objOrganizationChart.ParentID = null;

        OrgnizationChartDB objOrgaizationChartDB = new OrgnizationChartDB(ConnectionStrings.LMS);
        List<OrgnizationChart> lstOrganiztionChart = objOrgaizationChartDB.Select("", " where [ParentID] is NULL ");

        

        //lstOrganiztionChart = new List<OrgnizationChart>(from objOrg in lstOrganiztionChart where objOrg.StatusID != 2 select objOrg);


        lstOrganiztionChart = setNames(lstOrganiztionChart);

        ddlCompany.DataSource = lstOrganiztionChart;
        ddlCompany.DataTextField = "NameEN";
        ddlCompany.DataValueField = "ID";
        ddlCompany.DataBind();
    }




    /// <summary>
    /// this function will fill the business lines from database
    /// </summary>
    protected void bindBusiness()
    {
        OrgnizationChart objOrganizationChart = new OrgnizationChart();
        objOrganizationChart.ParentID = getMainOrg();

        OrgnizationChartDB objOrgaizationChartDB = new OrgnizationChartDB(ConnectionStrings.LMS);
        List<OrgnizationChart> lstOrganiztionChart = objOrgaizationChartDB.Search(objOrganizationChart);

        lstOrganiztionChart.Insert(0, new OrgnizationChart("-- اختار --", "-- Select --", null, 2,getMainOrg(),""));

        //lstOrganiztionChart = new List<OrgnizationChart>(from objOrg in lstOrganiztionChart where objOrg.StatusID != 2 select objOrg);


        lstOrganiztionChart = setNames(lstOrganiztionChart);

        ddlActivity.DataSource = lstOrganiztionChart;
        ddlActivity.DataTextField = "NameEN";
        ddlActivity.DataValueField = "ID";
        ddlActivity.DataBind();
    }


    /// <summary>
    /// this function will fill the Sectors from database
    /// </summary>
    protected void bindSector()
    {
       

        OrgnizationChartDB objOrgaizationChartDB = new OrgnizationChartDB(ConnectionStrings.LMS);
        List<OrgnizationChart> lstOrganiztionChart = objOrgaizationChartDB.getOrgnizationChartByParentID (long.Parse (ddlActivity.SelectedValue));


        lstOrganiztionChart.Insert(0, new OrgnizationChart("-- اختار --", "-- Select --", null, 2,getMainOrg(),""));
       // lstOrganiztionChart = new List<OrgnizationChart>(from objOrg in lstOrganiztionChart where objOrg.StatusID != 2 select objOrg);


        lstOrganiztionChart = setNames(lstOrganiztionChart);

        ddlSector.DataSource = lstOrganiztionChart;
        ddlSector.DataTextField = "NameEN";
        ddlSector.DataValueField = "ID";
        ddlSector.DataBind();
        
    }



    /// <summary>
    /// this function will fill the Managements from database
    /// </summary>
    protected void bindManagements()
    {


        OrgnizationChartDB objOrgaizationChartDB = new OrgnizationChartDB(ConnectionStrings.LMS);
        List<OrgnizationChart> lstOrganiztionChart = objOrgaizationChartDB.getOrgnizationChartByParentID(long.Parse(ddlSector.SelectedValue));


        lstOrganiztionChart.Insert(0, new OrgnizationChart("-- اختار --", "-- Select --", null, 2,getMainOrg(),""));
       // lstOrganiztionChart = new List<OrgnizationChart>(from objOrg in lstOrganiztionChart where objOrg.StatusID != 2 select objOrg);

        lstOrganiztionChart = setNames(lstOrganiztionChart);


        ddlManagement.DataSource = lstOrganiztionChart;
        ddlManagement.DataTextField = "NameEN";
        ddlManagement.DataValueField = "ID";
        ddlManagement.DataBind();
    }



    /// <summary>
    /// this function will fill the Divisions from database
    /// </summary>
    protected void bindDivisions()
    {


        OrgnizationChartDB objOrgaizationChartDB = new OrgnizationChartDB(ConnectionStrings.LMS);
        List<OrgnizationChart> lstOrganiztionChart = objOrgaizationChartDB.getOrgnizationChartByParentID(long.Parse(ddlManagement.SelectedValue));


        lstOrganiztionChart.Insert(0, new OrgnizationChart("-- اختار --", "-- Select --", null, 2,getMainOrg(),""));
       // lstOrganiztionChart = new List<OrgnizationChart>(from objOrg in lstOrganiztionChart where objOrg.StatusID != 2 select objOrg);

        lstOrganiztionChart = setNames(lstOrganiztionChart);


        ddlDivision.DataSource = lstOrganiztionChart;
        ddlDivision.DataTextField = "NameEN";
        ddlDivision.DataValueField = "ID";
        ddlDivision.DataBind();
    }



    /// <summary>
    /// this function will fill the Sections from database
    /// </summary>
    protected void bindSections()
    {


        OrgnizationChartDB objOrgaizationChartDB = new OrgnizationChartDB(ConnectionStrings.LMS);
        List<OrgnizationChart> lstOrganiztionChart = objOrgaizationChartDB.getOrgnizationChartByParentID(long.Parse(ddlDivision.SelectedValue));


        lstOrganiztionChart.Insert(0, new OrgnizationChart("-- اختار --", "-- Select --", null, 2,getMainOrg(),""));
        //lstOrganiztionChart = new List<OrgnizationChart>(from objOrg in lstOrganiztionChart where objOrg.StatusID != 2 select objOrg);

        lstOrganiztionChart = setNames(lstOrganiztionChart);


        ddlSection.DataSource = lstOrganiztionChart;
        ddlSection.DataTextField = "NameEN";
        ddlSection.DataValueField = "ID";
        ddlSection.DataBind();
    }




    protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindBusiness();
        bindSector();
        bindManagements();
        bindDivisions();
        bindSections();

        OnItemChanged(e);
    
    }


    protected void ddlActivity_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindSector();
        bindManagements();
        bindDivisions();
        bindSections();

        OnItemChanged(e);



    }
    protected void ddlSector_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindManagements();
        bindDivisions();
        bindSections();

        OnItemChanged(e);
    }
    protected void ddlManagement_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDivisions();
        bindSections();


        OnItemChanged(e);
    }
    protected void ddlDivision_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        bindSections();


        OnItemChanged(e);
    }
    protected void ddlSection_SelectedIndexChanged(object sender, EventArgs e)
    {
        OnItemChanged(e);
    }
}
